package cn;

import java.io.IOException;


import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Connection;

/**
 * Servlet implementation class Servlet
 */
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int name = Integer.valueOf(request.getParameter("name"));
		
		int number = Integer.valueOf(request.getParameter("number"));
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/jsp";
			String username = "root";
			String password = "root";
			java.sql.Connection conn = DriverManager.getConnection(url,username,password);
			String sql= "update tb_student set number=? where name=?";
			PreparedStatement ps = conn.prepareStatement(sql);			
			ps.setInt(1, number);		
			ps.setInt(2, name);
			ps.executeUpdate();
			ps.close();
			conn.close();
		}catch (Exception e) {
			e.printStackTrace();
		}response.sendRedirect("xiugai.jsp");
		
		doGet(request, response);
	}

}
